package com.jkl.controller.api;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.jkl.entity.BaseResponseDTO;
import com.jkl.entity.garbage.Garbage;
import com.jkl.service.garbage.inter.QueryGarbageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.concurrent.locks.*;
import java.util.List;


/**
 * @author liuzhennan
 * @create 2020-11-26 9:15
 */
@RestController
@RequestMapping("/jky/Api")
@Slf4j
public class GetGarbageApi {

    @Autowired
    QueryGarbageService queryGarbageService;

    @GetMapping("/getGarbageByName")
    public BaseResponseDTO getGarbageByName(@RequestParam(name = "gName") String gName ){
        BaseResponseDTO returnData = new BaseResponseDTO();
        if(gName == null || gName == ""){
            returnData.setResultCode("404");
            returnData.setResultMessage("fail");
            return returnData;
        }
        List<Garbage> garbage = queryGarbageService.queryGarbage(gName);
        JSONObject returnJson = new JSONObject();
        JSONArray garbageArray = (JSONArray) JSONArray.toJSON(garbage);
        if(garbage.size() !=0 &&garbage.get(0) != null){
            returnData.setResultCode("200");
        }else {
            returnData.setResultCode("404");
            returnData.setResultMessage("fail");
        }
        returnJson.put("garbageArray",garbageArray);
        return returnData.setReturnData(returnJson);
    }
}
